/**
 * Type definitions for Grep MCP Server
 */

// API related types - grep.app API parameters
export interface GrepAPIParams {
  q: string;
  'f.lang'?: string;
  'f.repo'?: string;
  'f.path'?: string;
}

// Gitee API parameters
export interface GiteeAPIParams {
  q: string;
  language?: string;
  repo?: string;
  path?: string;
}

// User-friendly search parameters
export interface SearchParams {
  query: string;
  language?: string;
  repo?: string;
  path?: string;
}

export interface SearchResponse {
  facets: {
    count: number;
    lang?: {
      buckets: Array<{
        val: string;
        count: number;
      }>;
    };
    repo?: {
      buckets: Array<{
        val: string;
        count: number;
      }>;
    };
  };
  hits: {
    hits: Array<{
      repo: { raw: string };
      path: { raw: string };
      branch: { raw: string };
      total_matches: { raw: string };
      content: {
        snippet: string;
      };
    }>;
  };
}

// Validation types
export interface ValidationResult {
  isValid: boolean;
  error?: string;
}

// Formatted response types
export interface LanguageStats {
  language: string;
  count: number;
}

export interface RepositoryStats {
  repository: string;
  count: number;
}

export interface FileMatch {
  file_path: string;
  branch: string;
  total_matches: number;
  line_numbers: number[];
  language: string;
  code_snippet: string;
}

export interface RepositoryGroup {
  repository: string;
  matches_count: number;
  files: FileMatch[];
}

export interface FormattedResponse {
  query: string;
  summary: {
    total_results: number;
    results_shown: number;
    repositories_found: number;
    top_languages: LanguageStats[];
    top_repositories: RepositoryStats[];
  };
  results_by_repository: RepositoryGroup[];
}

// Server configuration types
export interface ServerConfig {
  transport: 'stdio' | 'sse';
  host?: string;
  port?: number;
}

// Individual search result from grep.app API
export interface GrepSearchResult {
  repository: {
    name: string;
    branch: string;
  };
  path: {
    name: string;
  };
  content: {
    snippet: string;
  };
  matches?: number;
}

// Formatted search result for display
export interface FormattedSearchResult {
  repository: string;
  branch: string;
  filePath: string;
  language: string;
  lineNumbers: number[];
  totalMatches: number;
  snippet: string;
  cleanContent: string;
  url: string;
  contextLines: number;
}

// Custom error types
export class GrepAPIError extends Error {
  constructor(message: string) {
    super(message);
    this.name = 'GrepAPIError';
  }
}

export class GrepAPITimeoutError extends GrepAPIError {
  constructor(message: string = 'Request to grep.app API timed out') {
    super(message);
    this.name = 'GrepAPITimeoutError';
  }
}

export class GrepAPIRateLimitError extends GrepAPIError {
  constructor(message: string = 'Rate limit exceeded') {
    super(message);
    this.name = 'GrepAPIRateLimitError';
  }
}

// Gitee API error types
export class GiteeAPIError extends Error {
  constructor(message: string) {
    super(message);
    this.name = 'GiteeAPIError';
  }
}

export class GiteeAPITimeoutError extends GiteeAPIError {
  constructor(message: string = 'Request to Gitee API timed out') {
    super(message);
    this.name = 'GiteeAPITimeoutError';
  }
}

export class GiteeAPIRateLimitError extends GiteeAPIError {
  constructor(message: string = 'Gitee API rate limit exceeded') {
    super(message);
    this.name = 'GiteeAPIRateLimitError';
  }
}

// Gitee search response types
export interface GiteeSearchResponse {
  total_count: number;
  incomplete_results: boolean;
  items: Array<{
    name: string;
    path: string;
    sha: string;
    url: string;
    git_url: string;
    html_url: string;
    repository: {
      id: number;
      name: string;
      full_name: string;
      owner: {
        login: string;
        id: number;
        avatar_url: string;
        html_url: string;
      };
      html_url: string;
      description: string;
      language: string;
      default_branch: string;
    };
    score: number;
  }>;
}

// Search provider types
export type SearchProvider = 'github' | 'gitee';

export interface UnifiedSearchParams extends SearchParams {
  provider?: SearchProvider;
}
